package myTeam.timeShop.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import myTeam.timeShop.bean.TbUser;
import myTeam.timeShop.dao.UserDao;
import myTeam.timeShop.entity.User;
import myTeam.timeShop.service.UserService;
import myTeam.timeShop.util.MyMD5Util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("userService")
public class UserServiceImpl implements  UserService{
	@Autowired
	UserDao userDao;


	public TbUser load(String id) {

		return userDao.load(id);
	}


	public TbUser get(String id) {
		return userDao.get(id);
	}


	public List<TbUser> findAll() {

		return userDao.findAll();
	}


	public void persist(TbUser entity) {

		userDao.persist(entity);
	}


	public String save(TbUser entity) {

		return userDao.save(entity);
	}


	public void saveOrUpdate(TbUser entity) {

		userDao.saveOrUpdate(entity);
	}


	public void delete(String id) {

		userDao.delete(id);
	}


	public void flush() {

		userDao.flush();
	}


	@Override
	public List<TbUser> findByAccountAndPassword(String account, String password) {

		return userDao.findByAccountAndPassword( account, password);
	}


	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.UserService#isExistByUserAccount(java.lang.String)
	 */
	@Override

	public boolean isExistByUserAccount(String userAccount) {

		Long count = userDao.isExistByUserAccount(userAccount);
		if(count==0){
			return false;
		}else{
			return true;
		}
		
	}


	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.UserService#doSomeThingForUserRegister(java.lang.String, java.lang.String)
	 */
	@Override
	
	public void doSomeThingForUserRegister(String userAccount,
			String userPassword) {

		TbUser user = new TbUser();
			user.setUserAccount(userAccount);
			user.setUserPassword(MyMD5Util.toMD5String(userPassword));
			user.setUserExperience(0);
			user.setUserLastLoginTime(new Date());
			user.setUserLevel(0);
			this.save(user);
	}


	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.UserService#doSomeThingForUserLogin(java.lang.String)
	 */
	@Override
	
	public TbUser doSomeThingForUserLogin(String string) {
		TbUser user = this.get(string);
		Date lastLoginTime = user.getUserLastLoginTime();
		//时间格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		String lastLoginDay = sdf.format(lastLoginTime);
		String toDay = sdf.format(new Date());
		if(lastLoginDay.equals(toDay)){//代表是今天登录过的，所以不用加经验，需要判断等级变化
			int experience =user.getUserExperience();
			//判断是否加等级
			int level =user.getUserLevel();
			while(isAddLevel(experience, level)){//达到升级条件
				user.setUserLevel(user.getUserLevel()+1);
				level = level+1;
			}
			this.saveOrUpdate(user);
			//封装数据
			User clientUser = new User();
			clientUser.setUserAccount(user.getUserAccount());
			clientUser.setUserBirthday(user.getUserBirthday());
			clientUser.setUserExperience(user.getUserExperience());
			clientUser.setUserFacePath(user.getUserFacePath());
			clientUser.setUserId(user.getUserId());
			clientUser.setUserLastLoginTime(user.getUserLastLoginTime());
			clientUser.setUserLevel(user.getUserLevel());
			clientUser.setUserName(user.getUserName());
			clientUser.setUserPassword(user.getUserPassword());
			clientUser.setUserSchool(user.getUserSchool());
			clientUser.setUserSex(user.getUserSex());
			clientUser.setUserSign(user.getUserSign());
			return clientUser;//更新user返回
		}else{//代表今天还为登录过，要加经验和等级
			user.setUserExperience(user.getUserExperience()+1);//添加一点经验值
			int experience =user.getUserExperience();
			//判断是否加等级
			int level =user.getUserLevel();
			while(isAddLevel(experience, level)){//达到升级条件
				user.setUserLevel(user.getUserLevel()+1);
				level=level+1;
			}
			this.saveOrUpdate(user);
			//封装数据
			User clientUser = new User();
			clientUser.setUserAccount(user.getUserAccount());
			clientUser.setUserBirthday(user.getUserBirthday());
			clientUser.setUserExperience(user.getUserExperience());
			clientUser.setUserFacePath(user.getUserFacePath());
			clientUser.setUserId(user.getUserId());
			clientUser.setUserLastLoginTime(user.getUserLastLoginTime());
			clientUser.setUserLevel(user.getUserLevel());
			clientUser.setUserName(user.getUserName());
			clientUser.setUserPassword(user.getUserPassword());
			clientUser.setUserSchool(user.getUserSchool());
			clientUser.setUserSex(user.getUserSex());
			clientUser.setUserSign(user.getUserSign());
			return clientUser;//更新user返回
		}
		
	}


	/**
	 * 
	 * jiangjintai
	 * 2016年3月25日
	 * @param experience 当前经验
	 * @param level 当前等级
	 * 是否添加等级判断方法
	 */
	protected boolean isAddLevel(int experience, int level) {
		//计算下一个等级需要的经验
		int needExperience = 0;
		//计数器
		int sum = 0;
		for(int i=0;i<=level+1;i++){
			sum = sum +i;
		}
		needExperience = 5 * sum;
		if(experience>needExperience){//达到升级条件
			return true;
		}else{
			return false;
		}
	}


	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.UserService#getOtherUserMessage(java.lang.String)
	 */
	@Override
	
	public TbUser getOtherUserMessage(String userId) {
		TbUser tbUser = this.get(userId);
		User user = new User();
		user.setUserAccount(tbUser.getUserAccount());
		user.setUserBirthday(tbUser.getUserBirthday());
		user.setUserFacePath(tbUser.getUserFacePath());
		user.setUserId(tbUser.getUserId());
		user.setUserLevel(tbUser.getUserLevel());
		user.setUserName(tbUser.getUserName());
		user.setUserSchool(tbUser.getUserSchool());
		user.setUserSex(tbUser.getUserSex());
		user.setUserSign(tbUser.getUserSign());
		return user;
	}
}
